package com.tusuo.stds.ui.level;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.View;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.tusuo.stds.Constants;
import com.tusuo.stds.R;
import com.tusuo.stds.rest.LevelResult;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.integrity.IntegrityFragment_;
import com.tusuo.stds.ui.integrity.IntegrityResultFragment;
import com.tusuo.stds.ui.integrity.IntegrityResultFragment_;
import com.tusuo.stds.ui.main.TabEntity;
import com.tusuo.stds.ui.module.ModuleActivity_;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;

import java.util.ArrayList;
import java.util.List;

import myandroid.library.util.ApplicationHelper;


@EActivity(R.layout.activity_level_result)
public class LevelResultActivity extends BaseToolbarActivity<LevelResultView, LevelResultPresenter>
                                implements LevelResultView {

    @StringRes(R.string.toolbar_title_level_result)
    String toolbarTitle;

    @ViewById(R.id.level_result_tab)
    CommonTabLayout mTabLayout;

    private int mLevelRecordId;
    private String mLevelName;
    private int mLevelType; // 1:硬过模式; 10: 闯关模式
    private int mNextLevelId = 0;
    private int mLevelComplete;// 1 普通 2 自适应


    private String[] mTitles;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private ArrayList<Fragment> mFragmentList = new ArrayList<>();
    private int mLevelId;

    @AfterInject
    void init() {
        mLevelRecordId = getIntent().getIntExtra("levelRecordId", 0);
        mLevelName = getIntent().getStringExtra("levelName");
        mLevelType = getIntent().getIntExtra("levelType", 10);
        mLevelComplete = getIntent().getIntExtra("levelComplete",1);
        mLevelId = getIntent().getIntExtra("levelId", -1);
    }

    @AfterViews
    void afterViews() {
        setupUI(this);

        List<ToolbarOwner.MenuAction> menuActionList = new ArrayList<>();

        ToolbarOwner.Config config = new ToolbarOwner.Config(mLevelName, true, menuActionList);
        mToolbarOwner.setConfig(config);

        ApplicationHelper.getInstance().addActivity(this);
    }

    @NonNull
    @Override
    public LevelResultPresenter createPresenter() {
        return new LevelResultPresenter(this, mLevelRecordId);
    }

    /**
     * 重定义返回键操作
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.setClass(this, ModuleActivity_.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    public void setLevelResult(LevelResult levelResult) {
        mTabEntities = new ArrayList<>();
        mFragmentList = new ArrayList<>();
        mFragmentList.add(LevelResultFragment_.builder()
                .mLevelName(mLevelName)
                .mLevelRecordId(mLevelRecordId)
                .mLevelType(mLevelType)
                .levelResult(levelResult)
                .build());
        if (mLevelComplete == 1) {
            mTitles = new String[]{"闯关成绩"};
            mTabLayout.setVisibility(View.GONE);
        }else {
            mTitles = new String[]{"闯关成绩", "知识完整度"};
            mFragmentList.add(IntegrityResultFragment_.builder()
                    .levelResult(levelResult)
                    .mLevelType(Constants.CODE_COMPLETE_TYPE_LEVEL)
                    .mLevelId(mLevelId)
                    .build());
            mTabLayout.setVisibility(View.VISIBLE);
        }
        for (int i = 0; i < mTitles.length ; i ++) {
            mTabEntities.add(new TabEntity(mTitles[i], 0, 0));
        }
        if (mTabLayout.getTabCount() < 1) {
            mTabLayout.setTabData(mTabEntities,this,R.id.level_result_content,mFragmentList);
        }

        mTabLayout.notifyDataSetChanged();

    }

    @Override
    public void showTimeoutTip() {
    }

}
